﻿--[[
   @brief    游戏下架tips
   @author   hqzheng
   @version  1.0
   @date     2016-10-18
--]]



GameOffShellTips = {

};

GameOffShellTipsUIEventListener = {
};

function GameOffShellTips:new(parent_id, game_id)
    local o = {tips_id_ = parent_id, game_id_ = game_id};
    setmetatable(o, self);
    self.__index = self;
    o:init();
    return o;
end

function GameOffShellTips:init()
    self:log("init");
    self.tips_ctrl_ptr_ = ui_utility.get_ctrl_ptr_by_id(self.tips_id_);
    self.open_url_btn_id_ = ui_utility.get_ctrl_id_by_path(self.tips_id_, "BtnOpenUrl");
    self.web_game_tips_ctn_id_ = ui_utility.get_ctrl_id_by_path(self.tips_id_, "Ctn_TipsContent\\TipWebGame");
    self.mobie_game_tips_ctn_id_ = ui_utility.get_ctrl_id_by_path(self.tips_id_, "Ctn_TipsContent\\TipMobieGame");
end

function GameOffShellTips:log(msg, core_msg)
    base_utility.log(string.format("[GameOffShellTips] %s", msg), core_msg);
end

function GameOffShellTips:show(is_show)
    self:log(string.format("show, is_show:%s", tostring(is_show)));

    if self.tips_ctrl_ptr_ == nil then
        self:log("show, banner_ctrl_ptr_ is nil");
        return; 
    end

    if true == is_show then
        self.tips_ctrl_ptr_:MoveToCenter();
        self.tips_ctrl_ptr_:BringToTop();
        self.tips_ctrl_ptr_:Show(TRUE);
    elseif false == is_show then
        self.tips_ctrl_ptr_:Show(FALSE); 
    end
end

function GameOffShellTips:set_game_info(game_type, server_id, ver)
    self:log(string.format("[set_tips_info]:game_type%s ",
                          tostring(game_type)));
    self.game_type_ = game_type;
    self.server_id_ = server_id;
    self.version_ = ver;

    if ierd_tgp.game_library.GAME_LIST_TYPE_WEB == self.game_type_ then
        ui_utility.set_visible(self.web_game_tips_ctn_id_, true);
        ui_utility.set_visible(self.mobie_game_tips_ctn_id_, false);
        ui_utility.setproperty_by_id(self.open_url_btn_id_, "Caption", LoadStr("WEGAME_BANNER_DOWNLOAD_QQGAME"));
    elseif ierd_tgp.game_library.GAME_LIST_TYPE_PHONE == self.game_type_ then
        ui_utility.set_visible(self.mobie_game_tips_ctn_id_, true);
        ui_utility.set_visible(self.web_game_tips_ctn_id_, false);
        ui_utility.setproperty_by_id(self.open_url_btn_id_, "Caption", LoadStr("WEGAME_BANNER_DOWNLOAD_HELPER"));
    end
end

function GameOffShellTips:get_game_id()
    return self.game_id_;
end

function GameOffShellTips:open_url()
    if ierd_tgp.game_library.GAME_LIST_TYPE_WEB == self.game_type_ then
        web_utility.open_new_browser(banner_model.web_game_open_url, false, true);
    elseif ierd_tgp.game_library.GAME_LIST_TYPE_PHONE == self.game_type_ then
        web_utility.open_new_browser(banner_model.mobie_game_open_url, false, true);
    end
    self:show(false);
end

function GameOffShellTips:launch_game()
    local action_info = {
        game_id_ = self.game_id_,
        zone_id_ = self.server_id_,
        version_ = self.version_,
        uid_ = 0,
        launch_op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_OFF_SHELL_TIPS
    };

    WGGameAction.excute_action(GameActions.kStartGame, action_info);
    self:show(false);
end

function GameOffShellTips:open_announcement()
    web_utility.open_new_browser(banner_model.off_shell_announcement_url, false, true);
end

------------------------------GameOffShellTipsUIEventListener------------------------------------------------
function GameOffShellTipsUIEventListener.open_url(uilib, control_id, x, y)
    base_utility.log(string.format("[BigEventBannerUIEventListener]on_mouse_hover ctrl_id:%d",
        control_id));

    local ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local tips = banner_model.get_off_shell_tips(ctrl_id);
    if nil == tips then
        base_utility.log(string.format(
            "[GameOffShellTipsUIEventListener][open_url]tips is NIL,"..
            " ctrl_id:%d", control_id), true);
        return;
    end
    
    tips:open_url();
end

function GameOffShellTipsUIEventListener.launch_game(uilib, control_id, x, y)
    base_utility.log(string.format(
        "[BigEventBannerUIEventListener]on_mouse_leave ctrl_id:%d", control_id));
    
    local ctrl_id = ui_utility.get_ctrl_parent_id(control_id);
    local tips = banner_model.get_off_shell_tips(ctrl_id);
    if nil == tips then
        base_utility.log(string.format(
            "[GameOffShellTipsUIEventListener][open_url]tips is NIL,"..
            " ctrl_id:%d", control_id), true);
        return;
    end
    
    tips:launch_game();
end

function GameOffShellTipsUIEventListener.open_announcement(uilib, control_id, x, y)
    base_utility.log(string.format(
        "[GameOffShellTipsUIEventListener]open_announcement ctrl_id:%d", control_id));
    
    local tips_txt_id = ui_utility.get_ctrl_parent_id(control_id);
    local tips_game_ctn_id = ui_utility.get_ctrl_parent_id(tips_txt_id);
    local tips_ctn_id = ui_utility.get_ctrl_parent_id(tips_game_ctn_id);
    local tips_ctrl_id = ui_utility.get_ctrl_parent_id(tips_ctn_id);
    
    local tips = banner_model.get_off_shell_tips(tips_ctrl_id);

    if nil == tips then
        base_utility.log(string.format(
            "[GameOffShellTipsUIEventListener][open_url]tips is NIL,"..
            " ctrl_id:%d", control_id), true);
        return;
    end
    
    tips:open_announcement();
end

function GameOffShellTipsUIEventListener.close_tips(uilib, control_id, x, y)
    base_utility.log(string.format(
        "[GameOffShellTipsUIEventListener]close_tips ctrl_id:%d", control_id));
    
    local title_bar_id = ui_utility.get_ctrl_parent_id(control_id);
    local tips_ctrl_id = ui_utility.get_ctrl_parent_id(title_bar_id);
    local tips = banner_model.get_off_shell_tips(tips_ctrl_id);

    if nil == tips then
        base_utility.log(string.format(
            "[GameOffShellTipsUIEventListener][open_url]tips is NIL,"..
            " ctrl_id:%d", control_id), true);
        return;
    end
    
    tips:show(false);
end

